const Router = require('koa-router');
const { testUsr } = require('../common');
const uuid = require('../common/uuid');
let router = new Router();

router.post('*', async (ctx, next) => {
  let { tel, psw } = ctx.request.body;
  let ID = uuid();
  console.log('aaawwwwwwwwwwwww');
  //检测用户名
  if (testUsr(tel).code == 1) {
    if (
      (await ctx.db.select('user_table', 'username', {
        username: tel
      }))[0]
    ) {
      ctx.body = { code: 0, msg: '该手机号已被注册!' };
    } else {
      try {
        await ctx.db.insert('user_table', {
          username: tel,
          psw,
          ID,
          register_time: Math.round(new Date().getTime() / 1000)
        });
        ctx.session.user_symbol = uuid();
        await ctx.db.insert('sess_table', {
          user_symbol: ctx.session.user_symbol,
          username: tel,
          btc: 0,
          usdt: 2000,
          eth: 0,
          ltc: 0,
          mycoin: 20000
        });

        ctx.body = { code: 1, msg: { register_time: new Date().getTime() } };
      } catch (error) {
        console.log(error);
        ctx.body = { code: 0, msg: '数据库错误' };
      }
    }
  } else {
    ctx.body = testUsr(tel);
  }
  // ctx.db.insert();
});
router.get('*', async ctx => {
  console.log('aaaa');
  //如果只是单页页面刷新
  ctx.redirect('/');
});

module.exports = router.routes();
